/***************************************************************************
 *            screen.c
 *
 *  Mon Aug 25 16:38:05 2008
 *  Copyright  2008  Tiago Falcão
 *  <tiago@tiagofalcao.com>
 ****************************************************************************/

/*
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Library General Public License for more details.
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor Boston, MA 02110-1301,  USA
 */
#include "screen.h"



//Inicialização da Tela
void screen(int x,int y,int w,int h,char * title){
	//Objetos
	objs=objects_manager_create();
	//texmanager=texture_manager_create();
	//Double Buffer, RGBA e Depth
	glutInitDisplayMode (GLUT_DOUBLE | GLUT_RGBA | GLUT_DEPTH);
	glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
	//Transparencia
	glBlendFunc(GL_SRC_ALPHA, GL_ONE);//_MINUS_SRC_ALPHA
	//Tamanho
	glutInitWindowSize (w, h);
	//Posição
	glutInitWindowPosition (x, y);
	//Cria Janela e seta Titulo
	glutCreateWindow (title);
    // Especifica que a cor de fundo da janela será preta
	glClearColor(0.0, 0.0, 0.0, 1.0);
	/****************** TEXTURA  *********************/
	//Textura
	{
		GLfloat PlaneS[] = { 10., 0., .0, 0. };
		GLfloat PlaneT[] = { 0., -10., .0, 0. };
	    //glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
		
	    //glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S,  GL_REPEAT);
	    //glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T,  GL_REPEAT);
	    //glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_R,  GL_REPEAT);
	    glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER,  GL_NEAREST);
	    glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER,  GL_LINEAR_MIPMAP_NEAREST);
	    glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_BLEND);
		
	    glTexGeni(GL_S, GL_TEXTURE_GEN_MODE, GL_OBJECT_LINEAR);
	    glTexGeni(GL_T, GL_TEXTURE_GEN_MODE, GL_OBJECT_LINEAR);
	    //glTexGeni(GL_R, GL_TEXTURE_GEN_MODE, GL_OBJECT_LINEAR);
		glTexGenfv(GL_S, GL_OBJECT_PLANE, PlaneS);
		glTexGenfv(GL_T, GL_OBJECT_PLANE, PlaneT);
	}

	/****************** LUZ  *********************/
	light_init();
	
	/****************** FUNCOES  *********************/
	//Seta função de desenho
	glutDisplayFunc (display);
	//Inicializa Camera
	cam_init();
	//Seta função de redimensionamento
	glutReshapeFunc (reshape_listener);
	//Listener do Teclado
	glutKeyboardFunc(key_listener);
	glutSpecialFunc(special_key_listener);
	//Listener do Mouse
	glutMouseFunc(mouse_listener);
	//Listener do Scroll do Mouse
	glutMouseWheelFunc(mouse_wheel_listener);
	//Cria Menu
	menu_create();
	//Close Fuction
	glutWMCloseFunc(end_program);
	
	//Default
	menu_draw_obj_listener(2);
	menu_screen_listener(MENU_OBJ_SELECT);
	menu_texture_listener(9);
	menu_mode_listener(OBJ3D_MODE_FILL); //Flat
	menu_object_listener(MENU_OBJ_UNSELECT);
	
	//Show
	glutMainLoop ();
}

//Funcao de Desenho
void display(){
	int start=glutGet(GLUT_ELAPSED_TIME),end;
	//Limpeza
	glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

	// Re-define os parâmetros da luz
	light_setup();
	
	// Habilita o modelo de colorização de Gouraud
	glShadeModel(GL_SMOOTH);
	glEnable(GL_LINE_SMOOTH);
	// Habilita a definição da cor do material a partir da cor corrente
	glEnable(GL_COLOR_MATERIAL);
	// Habilita o depth-buffering
	glEnable(GL_DEPTH_TEST);
	glDepthFunc(GL_LEQUAL);
	glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
	// Habilita Transparencia
	glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
	glEnable(GL_BLEND);
	
	//Desenha Objetos 3D
	objects_manager_display(objs);

	//Troca Buffers
	glutSwapBuffers();
	
	end=glutGet(GLUT_ELAPSED_TIME);
	printf("Taxa: %.3f fps (%d ms)\n", 1000.0/(end-start), end-start);
}

//Listener do Tamanho da Janela
void reshape_listener (GLsizei w, GLsizei h) {
	// Para previnir uma divisão por zero
	if ( h == 0 ) h = 1;

	// Especifica o tamanho da viewport
	glViewport(0, 0, w, h);
	
	// Calcula a correção de aspecto
	cam_aspect((GLfloat)w/(GLfloat)h);
	
	//Reconfigura Camera
	cam_setup();
}

//Função de Saida
void end_program(){
	objects_manager_delete(objs);
	menu_delete();
	//texture_manager_delete(texmanager);
	exit(0);
}
